package com.awa.usercenter.model.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class Code implements Serializable, UserDetails {
    private String userPhone;
    private Set<UserRole> roleSet;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<String> roleName = new HashSet<>();
        for (UserRole userRole : roleSet) {
            roleName.add(userRole.getRoleName());
        }
        if(!roleName.isEmpty()){
            return roleName.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
        }
        return null;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return userPhone;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
